package com.controller;

import com.comment.CommentResult;
import com.domain.entity.Cart;
import com.domain.req.AddCartReq;
import com.domain.req.PageReq;
import com.domain.req.UpdateGoodsSumReq;
import com.github.pagehelper.PageInfo;
import com.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车 交互层
 */
@CrossOrigin
@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 新增购物车信息
     */
    @PostMapping("/addCart")
    public CommentResult<Void> addCart(@RequestBody AddCartReq addCartReq) {
        cartService.addCart(addCartReq);
        return CommentResult.success();
    }

    /**
     * 查询本人的所有购物车信息
     */
    @GetMapping("/list")
    public CommentResult<List<Cart>> list() {
        List<Cart> list = cartService.list();
        return CommentResult.success(list);
    }

    /**
     * 根据购物车id删除购物车信息
     */
    @DeleteMapping("/delete/{id}")
    public CommentResult<Void> delete(@PathVariable("id") Integer id) {
        cartService.deleteById(id);
        return CommentResult.success();
    }

    /**
     * 清空购物车信息
     */
    @DeleteMapping("/deleteAll")
    public CommentResult<Void> deleteAll() {
        cartService.deleteAll();
        return CommentResult.success();
    }

    /**
     * 修改购物车商品数量
     */
    @PutMapping("/updateGoodsSum")
    public CommentResult<Void> updateGoodsSum(@RequestBody UpdateGoodsSumReq updateGoodsSumReq) {
        cartService.updateGoodsSum(updateGoodsSumReq);
        return CommentResult.success();
    }

    /**
     * 分页查询购物车信息
     */
    @GetMapping("/pageList")
    public CommentResult<PageInfo<Cart>> pageList(PageReq pageReq) {
        PageInfo<Cart> cartPageInfo = cartService.pageList(pageReq);
        return CommentResult.success(cartPageInfo);
    }

    @GetMapping("/getTotalMoney")
    public CommentResult<Double> getTotalMoney() {
        Double totalMoney = cartService.getTotalMoney();
        return CommentResult.success(totalMoney);
    }

}
